//
//  AppDelegate.m
//  DanKU
//
//  Created by 吴斌 on 2023/9/18.
//

#import "AppDelegate.h"
#import "DKTabBarViewController.h"
#import "DKRegisterViewController.h"

#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>
#import "DKAuthorityHelper.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[IQKeyboardManager sharedManager] setEnable:true];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:true];
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = true;
    
    [self makeInitSet];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [DKPublicHttp apiUserRsaKeyForAPI:@{} complete:^{
        NSLog(@"公钥:%@",[DKGlobalHelper defaultManager].userRsaKey);
    }];//获取公钥配置
    
    DKRegisterUserModel * dkRegisterUserMdl = [DKGlobalHelper getRegisterUserInfo];
    if(dkRegisterUserMdl&&dkRegisterUserMdl.token.length){
        DKTabBarViewController * dk_TabBarViewCtrl = [[DKTabBarViewController alloc] init];
        dk_TabBarViewCtrl.isAuth = NO;
        self.window.rootViewController = dk_TabBarViewCtrl;
    }else{
        DKRegisterViewController * dk_RegisterViewCtrl = [[DKRegisterViewController alloc] init];
        dk_RegisterViewCtrl.isNeedBack = false;
        UINavigationController *dk_RegisterViewNavCtrl = [[UINavigationController alloc] initWithRootViewController:dk_RegisterViewCtrl];
        self.window.rootViewController = dk_RegisterViewNavCtrl;
    }
    [self.window makeKeyAndVisible];
    [self makeLocationAuth];
    [self makeAppAdjust];
    return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    if (@available(iOS 14, *)) {
        ATTrackingManagerAuthorizationStatus status = ATTrackingManager.trackingAuthorizationStatus;
        switch (status) {
            case ATTrackingManagerAuthorizationStatusDenied:
                //用户拒绝IDFA
                break;
            case ATTrackingManagerAuthorizationStatusAuthorized:{
                //用户允许IDFA
                if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
                    //                    NSLog(@"%@",idfaString);
                }
            }
                break;
            case ATTrackingManagerAuthorizationStatusNotDetermined: {
                //用户未做选择或未弹窗IDFA
                //请求弹出用户授权框，只会在程序运行是弹框1次，除非卸载app重装，通地图、相机等权限弹框一样
                [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
                    if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
                        // 你的操作
                    }
                    
                }];
            }
                break;
            default:
                break;
        }
    } else {
        // Fallback on earlier versions
        if ([ASIdentifierManager.sharedManager isAdvertisingTrackingEnabled]) {
        }else {
        }
    }
    [Adjust trackSubsessionStart];
}
-(void)applicationWillResignActive:(UIApplication *)application{
    [Adjust trackSubsessionEnd];
}
- (void)makeLocationAuth{
    [DKAuthorityHelper requestLocationAuthorization:NO withAuthorization:^(DKAuthorizationStatus status) {
        switch (status) {
            case DKAuthorizationStatusAuthorized:{//已授权
                
            }
                break;
            default:{
                UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"" message:@"Your Location permission is disabled.We must get your permission to access your location data in order to determine whether there is a fraud risk. turn on the applied Location permission in settings." preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                }];
                UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                }];
                [alertCtrl addAction:cancelAction];
                [alertCtrl addAction:okAction];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [((AppDelegate*)([UIApplication sharedApplication].delegate)).window.rootViewController presentViewController:alertCtrl animated:YES completion:nil];
                });
            }
                break;
        }
    }];
}
- (void)makeInitSet{
    NSString * userRandomKey = DK_GETObject_USERDEF(NSUserDefault_UserRandomKey);
    if(!userRandomKey.length){
        [DKGlobalHelper defaultManager].userRandomKey = [NSString getSelfUuid];
        if(![DKGlobalHelper defaultManager].userRandomKey.length){
            [DKGlobalHelper defaultManager].userRandomKey = @"";
        }
        DK_SETObject_USERDEF(NSUserDefault_UserRandomKey, [DKGlobalHelper defaultManager].userRandomKey);
    }else{
        [DKGlobalHelper defaultManager].userRandomKey = userRandomKey;
    }
    NSString * selfUuid = DK_GETObject_USERDEF(NSUserDefault_SelfUuid);
    if(!selfUuid.length){
        [DKGlobalHelper defaultManager].selfUuid = [NSString getSelfUuid];
        if(![DKGlobalHelper defaultManager].selfUuid.length){
            [DKGlobalHelper defaultManager].selfUuid = @"";
        }
        DK_SETObject_USERDEF(NSUserDefault_SelfUuid, [DKGlobalHelper defaultManager].selfUuid);
    }else{
        [DKGlobalHelper defaultManager].selfUuid = selfUuid;
    }
}
- (void)makeAppAdjust{
    NSString *environment = ADJEnvironmentSandbox;
#ifdef DEBUG
    environment = ADJEnvironmentSandbox;
#else
    environment = ADJEnvironmentProduction;
#endif
    ADJConfig * adjustConfig =  [ADJConfig configWithAppToken:DK_AdjustToken environment:environment];
    [Adjust appDidLaunch:adjustConfig];
    //第一次打点
    [[DKAdjustHelper defaultManager] adjustFirstApp];
}
@end
